package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendPunchStaffDAO;
import org.jsola.hr.dto.AttendPunchStaffAddDTO;
import org.jsola.hr.dto.AttendPunchStaffUpdateDTO;
import org.jsola.hr.entity.AttendPunchStaffDO;
import org.jsola.hr.query.AttendPunchStaffQuery;
import org.jsola.hr.service.IAttendPunchStaffService;
import org.jsola.hr.vo.AttendPunchStaffListVO;
import org.jsola.hr.vo.AttendPunchStaffVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendPunchStaffServiceImpl")
public class AttendPunchStaffServiceImpl implements IAttendPunchStaffService {

    @Autowired
    private IAttendPunchStaffDAO attendPunchStaffDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendPunchStaffVO save(AttendPunchStaffAddDTO attendPunchStaffAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendPunchStaffDO attendPunchStaffDO = attendPunchStaffAddDTO.to(AttendPunchStaffDO.class);
        // 保存
        attendPunchStaffDO = save(attendPunchStaffDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendPunchStaffDO.to(AttendPunchStaffVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendPunchStaffUpdateDTO attendPunchStaffUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendPunchStaffDO attendPunchStaffDO = attendPunchStaffUpdateDTO.to(AttendPunchStaffDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendPunchStaffDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendPunchStaffIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendPunchStaffIds);
    }


    @Override
    public AttendPunchStaffVO selectById(String attendPunchStaffId, String siteId) {
        AttendPunchStaffDO attendPunchStaffDO = selectDOById(attendPunchStaffId, siteId);
        if (attendPunchStaffDO == null) {
            return null;
        }
        return attendPunchStaffDO.to(AttendPunchStaffVO.class);
    }

    @Override
    public List<AttendPunchStaffListVO> select(AttendPunchStaffQuery attendPunchStaffQuery, String siteId) {
        List<AttendPunchStaffDO> attendPunchStaffDOList = selectDO(attendPunchStaffQuery, siteId);
        if(CollectionUtils.isEmpty(attendPunchStaffDOList)) {
            return attendPunchStaffDOList == null ? null : new ArrayList<>();
        }
        return attendPunchStaffDOList.stream()
                .map(attendPunchStaffDO -> attendPunchStaffDO.to(AttendPunchStaffListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendPunchStaffQuery attendPunchStaffQuery, String siteId) {
        Example example = buildExample(attendPunchStaffQuery,siteId);
        return attendPunchStaffDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendPunchStaffListVO> selectPage(AttendPunchStaffQuery attendPunchStaffQuery, String siteId) {
        Example example = buildExample(attendPunchStaffQuery,siteId);
        Page<AttendPunchStaffDO> page = attendPunchStaffDAO.selectPageByExample(example,
                attendPunchStaffQuery.getPageNo(),
                attendPunchStaffQuery.getPageSize());

        return page.to(AttendPunchStaffListVO.class);
    }

    @Override
    public AttendPunchStaffDO selectDOById(String attendPunchStaffId, String siteId) {
        return listById(attendPunchStaffId, siteId);
    }

    @Override
    public List<AttendPunchStaffDO> selectDO(AttendPunchStaffQuery attendPunchStaffQuery, String siteId) {
        Example example = buildExample(attendPunchStaffQuery,siteId);
        return attendPunchStaffDAO.selectByExample(example);
    }

    @Override
    public int deleteByAttendPlanId(String attendPlanId, String siteId) {
        AttendPunchStaffQuery attendPunchStaffQuery = new AttendPunchStaffQuery();
        attendPunchStaffQuery.setAttendPlanId(attendPlanId);
        Example example = buildExample(attendPunchStaffQuery,siteId);
        return attendPunchStaffDAO.deleteByExample(example);
    }

    @Override
    public int logicDelByAttendPlanId(String attendPlanId, TokenUser tokenUser) {

        // 查询
        AttendPunchStaffQuery attendPunchStaffQuery = new AttendPunchStaffQuery();
        attendPunchStaffQuery.setAttendPlanId(attendPlanId);
        List<AttendPunchStaffDO> dataList = this.selectDO(attendPunchStaffQuery, tokenUser.getSiteId());
        if (dataList.size() <= 0) {
            return 0;
        }

        for (AttendPunchStaffDO attendPunchStaff : dataList) {

            AttendPunchStaffDO attendPunchStaffDO = new AttendPunchStaffDO();

            attendPunchStaffDO.setId(attendPunchStaff.getId());
            attendPunchStaffDO.setValid(false);
            attendPunchStaffDO.setModifiedUserId(tokenUser.getUserId());
            attendPunchStaffDO.setGmtModified(new Date());
            attendPunchStaffDAO.updateByPrimaryKeySelective(attendPunchStaffDO);
        }

        return 0;
    }


    /**
     * 根据查询参数，构建example

     * @param attendPunchStaffQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendPunchStaffQuery attendPunchStaffQuery, String siteId) {
        Example example = new Example(AttendPunchStaffDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendPunchStaffQuery.getCompanyId() != null) {
            example.and().andLike("companyId", "%" + attendPunchStaffQuery.getCompanyId() + "%");
        }
        if (attendPunchStaffQuery.getCompanyName() != null) {
            example.and().andLike("companyName", "%" + attendPunchStaffQuery.getCompanyName() + "%");
        }
        if (attendPunchStaffQuery.getDeptId() != null) {
            example.and().andLike("deptId", "%" + attendPunchStaffQuery.getDeptId() + "%");
        }
        if (attendPunchStaffQuery.getDeptName() != null) {
            example.and().andLike("deptName", "%" + attendPunchStaffQuery.getDeptName() + "%");
        }
        if (attendPunchStaffQuery.getAttendPlanId() != null) {
            example.and().andLike("attendPlanId", "%" + attendPunchStaffQuery.getAttendPlanId() + "%");
        }
        if (attendPunchStaffQuery.getAttendPlanIdNotEq() != null) {
        	example.and().andNotEqualTo("attendPlanId", attendPunchStaffQuery.getAttendPlanIdNotEq());
        }
        if (attendPunchStaffQuery.getEmpInfoId() != null) {
            example.and().andLike("empInfoId", "%" + attendPunchStaffQuery.getEmpInfoId() + "%");
        }
        if (attendPunchStaffQuery.getEmpInfoName() != null) {
            example.and().andLike("empInfoName", "%" + attendPunchStaffQuery.getEmpInfoName() + "%");
        }
        if (attendPunchStaffQuery.getGmtCreateStart() != null &&
                attendPunchStaffQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendPunchStaffQuery.getGmtCreateStart(),
                    attendPunchStaffQuery.getGmtCreateEnd());
        }
        if (attendPunchStaffQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendPunchStaffQuery.getCreateUserId() + "%");
        }
        if (attendPunchStaffQuery.getGmtModifiedStart() != null &&
                attendPunchStaffQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendPunchStaffQuery.getGmtModifiedStart(),
                    attendPunchStaffQuery.getGmtModifiedEnd());
        }
        if (attendPunchStaffQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendPunchStaffQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,attendPunchStaffQuery.getOrders());
        return example;
    }
}




